package rm2;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.StringTokenizer;

public class MRMessage 
{
	HashMap<String, Integer> montreal;// to have montreal inventory
	HashMap<String, Integer> vancouver;// to have vancouver inventory
	HashMap<String, Integer> toronto;// to have toronto inventory
	String RequestType;// request type from the Replica Manager getState or upData 
	String packet;//basiaclly the message
	int Sequencer;
	
	public MRMessage()
	{
		super();
		this.montreal = new HashMap<String, Integer>();
		this.vancouver =new HashMap<String, Integer>();
		this.toronto = new HashMap<String, Integer>();
		this.RequestType = "getstate";
		this.packet = "";
		this.Sequencer = 0;
		initHashes();
	}
	
	public void initHashes()
	{
		montreal.put("1111",new Integer(0));
		montreal.put("2222",new Integer(0));
		montreal.put("3333",new Integer(0));
		montreal.put("4444",new Integer(0));
		/////////////////////////////////////
		vancouver.put("1111",new Integer(0));
		vancouver.put("2222",new Integer(0));
		vancouver.put("3333",new Integer(0));
		vancouver.put("4444",new Integer(0));
		/////////////////////////////////////
		toronto.put("1111",new Integer(0));
		toronto.put("2222",new Integer(0));
		toronto.put("3333",new Integer(0));
		toronto.put("4444",new Integer(0));
		
	}
	public int getSequencer() {
		return Sequencer;
	}

	public void setSequencer(int sequencer) {
		Sequencer = sequencer;
	}

	public String genrateMessage()
	{
		// TODO Auto-generated method stub
		return packet;
	}
	
	public void parseMessage(String mes)
	{
		String string = mes;
		StringTokenizer st = new StringTokenizer(string, "|");
	      int index=0;
	      //Message m=new Message();
	      while (st.hasMoreTokens()) 
	      {
	          index++;
	          String token = st.nextToken();
	          System.out.println(token + "  " + index);
	          switch(index)
	          {
	          case 1:
	        	  this.setSequencer(Integer.parseInt(token));
	        	  break;
	          case 2:
	        	  this.setRequestType(token);
	        	  break;
	          case 3:
	        	  this.montreal.put("1111", Integer.parseInt(token));
	        	  break;
	          case 4:
	        	  this.montreal.put("2222", Integer.parseInt(token));
	        	  break;
	          case 5:
	        	  this.montreal.put("3333", Integer.parseInt(token));
	        	  break;
	          case 6:
	        	  this.montreal.put("4444", Integer.parseInt(token));
	        	  break;
	          case 7:
	        	  this.vancouver.put("1111", Integer.parseInt(token));
	        	  break;
	          case 8:
	        	  this.vancouver.put("2222", Integer.parseInt(token));
	        	  break;
	          case 9:
	        	  this.vancouver.put("3333", Integer.parseInt(token));
	        	  break;
	          case 10:
	        	  this.vancouver.put("4444", Integer.parseInt(token));
	        	  break;
	          case 11:
	        	  this.toronto.put("1111", Integer.parseInt(token));
	        	  break;
	          case 12:
	        	  this.toronto.put("2222", Integer.parseInt(token));
	        	  break;
	          case 13:
	        	  this.toronto.put("3333", Integer.parseInt(token));
	        	  break;
	          case 14:
	        	  this.toronto.put("4444", Integer.parseInt(token));
	        	  break;
	        	  
	          }
	      }

	}
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		MRMessage s = new MRMessage();
		HashMap<String , Integer> m = new HashMap<String, Integer>();
		m.put("1111", new Integer(20));
		m.put("2222", new Integer(20));
		m.put("3333", new Integer(20));
		m.put("4444", new Integer(20));
		s.setMontreal(m);
		HashMap<String , Integer> v = new HashMap<String, Integer>();
		v.put("1111", new Integer(40));
		v.put("2222", new Integer(40));
		v.put("3333", new Integer(40));
		v.put("4444", new Integer(40));
		s.setVancouver(v);
		HashMap<String , Integer> t = new HashMap<String, Integer>();
		t.put("1111", new Integer(80));
		t.put("2222", new Integer(80));
		t.put("3333", new Integer(80));
		t.put("4444", new Integer(80));
		s.setToronto(t);
		System.out.println(s.getPacket());
		MRMessage s1 = new MRMessage();
		s1.parseMessage(s.getPacket());
		System.out.println("__________________________________________________");
		byte [] byerts =  s.getPacket().getBytes();
		
		MRMessage test = new MRMessage();
		test.parseMessage( new String(byerts, Charset.defaultCharset()));
		System.out.println(test.getMontreal());
		System.out.println(test.getVancouver());
		System.out.println(test.getToronto());
		System.out.println(test.getRequestType());
		System.out.println(test.getSequencer());

	}


	public HashMap<String, Integer> getMontreal() 
	{
		return montreal;
	}


	public void setMontreal(HashMap<String, Integer> montreal) 
	{
		this.montreal = montreal;
	}


	public HashMap<String, Integer> getVancouver() 
	{
		return vancouver;
	}


	public void setVancouver(HashMap<String, Integer> vancouver) 
	{
		this.vancouver = vancouver;
	}


	public HashMap<String, Integer> getToronto() 
	{
		return toronto;
	}


	public void setToronto(HashMap<String, Integer> toronto) 
	{
		this.toronto = toronto;
	}


	public String getRequestType() 
	{
		return RequestType;
	}


	public void setRequestType(String requestType) {
		RequestType = requestType;
	}


	public String getPacket() 
	{
		String temp = "|";
		temp = temp + getSequencer() + "|";
		temp = temp + getRequestType() + "|";
		/** First add Montreal */
		temp = temp + montreal.get("1111") + "|";
		temp = temp + montreal.get("2222") + "|";
		temp = temp + montreal.get("3333") + "|";
		temp = temp + montreal.get("4444") + "|";
		/** Second add vancouver */
		temp = temp + vancouver.get("1111") + "|";
		temp = temp + vancouver.get("2222") + "|";
		temp = temp + vancouver.get("3333") + "|";
		temp = temp + vancouver.get("4444") + "|";
		/** Third add toronto */
		temp = temp + toronto.get("1111") + "|";
		temp = temp + toronto.get("2222") + "|";
		temp = temp + toronto.get("3333") + "|";
		temp = temp + toronto.get("4444") + "|";
		
		return temp;
	}


	public void setPacket(String packet) {
		this.packet = packet;
	}

}
